package com.fly.show.casco;

import java.io.InputStream;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Cursor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;

public class StartUp
{
    private Display display;
    
    private Shell shell;
    
    /**
     * Create the shell.
     * 
     * @param display
     */
    public StartUp()
    {
        display = Display.getDefault();
        shell = new Shell(display, SWT.NONE);
        shell.setText("ABC");
        createContents();
        open();
    }
    
    /**
     * Create contents of the shell.
     */
    private void createContents()
    {
        InputStream is = this.getClass().getResourceAsStream("/img/start.jpg");
        Image image = new Image(Display.getDefault(), is);
        Canvas canvas = new Canvas(shell, SWT.ON_TOP);
        canvas.setBackgroundImage(image);
        canvas.setBounds(0, 0, image.getBounds().width, image.getBounds().height);
        shell.setSize(image.getBounds().width, image.getBounds().height);
    }
    
    /**
     * Open the window.
     */
    private void open()
    {
        // 设置图标
        InputStream is = this.getClass().getResourceAsStream("/img/icon.gif");
        if (is != null)
        {
            shell.setImage(new Image(display, is));
        }
        // 设置默认居中
        Rectangle screeRec = display.getBounds();
        Rectangle shellRec = shell.getBounds();
        if (shellRec.height > screeRec.height)
        {
            shellRec.height = screeRec.height;
        }
        if (shellRec.width > screeRec.width)
        {
            shellRec.width = screeRec.width;
        }
        shell.setLocation((screeRec.width - shellRec.width) / 2, (screeRec.height - shellRec.height) / 2);
        shell.setCursor(new Cursor(display, SWT.CURSOR_WAIT));
        shell.open();
        shell.layout();
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
        display.dispose();
    }
    
    /**
     * Launch the application.
     * 
     * @param args
     */
    public static void main(String args[])
    {
        new StartUp();
    }
}
